package com.mkh.st.admin.strategy;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.mkh.st.admin.api.entity.SysBusinessEntity;
import com.mkh.st.admin.api.entity.SysUserRoleScope;
import com.mkh.st.admin.api.vo.DataPermissionVO;
import com.mkh.st.admin.api.vo.ScopeDataDeatil;
import com.mkh.st.admin.api.vo.ScopeDataVO;
import com.mkh.st.admin.api.vo.UserRoleScopeVO;
import com.mkh.st.admin.mapper.SysBusinessEntityMapper;
import com.mkh.st.common.core.constant.CommonConstants;
import com.mkh.st.warehouse.api.feign.IWarehouseProvider;
import com.mkh.st.warehouse.dto.DataPermissionDTO;
import com.mkh.st.warehouse.vo.WarehouseFeignVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component
public class WarehouseStrategy extends BaseScopeStrategy{

	private final List<Long> ids= Arrays.asList(-1L);

	@Autowired
	private IWarehouseProvider warehouseProvider;

	@Autowired
	private SysBusinessEntityMapper businessEntityMapper;

	private final Map<Long,String> siteMap=new ConcurrentHashMap<>();

	@Override
	public void setDataPermission(DataPermissionVO vo, SysUserRoleScope sysUserRoleScope, Long tenantId) {
		
		if(ObjectUtil.isNotNull(sysUserRoleScope)){
			DataPermissionDTO dto=new DataPermissionDTO();
			if(CommonConstants.ONE.equals(sysUserRoleScope.getAllFlag())){
				vo.setWarehouseAll(Boolean.TRUE);
				dto.setAllFlag(Boolean.TRUE);
				dto.setTenantId(tenantId);
				List<WarehouseFeignVO> list = warehouseProvider.getDataPermWarehouse(dto).getData();
				if(CollectionUtil.isNotEmpty(list)){
					List<Long> warehouseIds=list.stream().map(WarehouseFeignVO::getId).collect(Collectors.toList());
					vo.setWarehouseList(warehouseIds);
				}
			}else {
				if(StrUtil.isNotBlank(sysUserRoleScope.getIds())) {
					List<Long> allWarehouseIds = Convert.toList(Long.class, StrUtil.split(sysUserRoleScope.getIds(), ','));
					//未删除未禁用查询
					dto.setAllFlag(Boolean.FALSE);
					dto.setIds(allWarehouseIds);
					dto.setTenantId(tenantId);
					List<WarehouseFeignVO> list = warehouseProvider.getDataPermWarehouse(dto).getData();
					if (CollectionUtil.isNotEmpty(list)) {
						List<Long> warehouseIds = list.stream().map(WarehouseFeignVO::getId).collect(Collectors.toList());
						vo.setWarehouseList(warehouseIds);
					} else {
						vo.setWarehouseList(ids);
					}
				}else {
					vo.setWarehouseList(ids);
				}
			}
		}else {
			vo.setWarehouseList(ids);
		}
	}

	@Override
	public Long setKey() {
		return 7L;
	}

	@Override
	public ScopeDataDeatil getScopeDataList(DataPermissionVO vo, SysUserRoleScope one,Long scopeId) {
		ScopeDataDeatil deatil=new ScopeDataDeatil();
		deatil.setAllFlag(vo.getWarehouseAll());
		deatil.setScopeId(scopeId);
		List<ScopeDataVO> scopeList =new ArrayList<>();
		if(CollectionUtil.isEmpty(vo.getWarehouseList())){
			return deatil;
		}
		DataPermissionDTO dto=new DataPermissionDTO();
		//当前操作人的仓库权限
		dto.setAllFlag(Boolean.FALSE);
		dto.setIds(vo.getWarehouseList());
		List<WarehouseFeignVO> list = warehouseProvider.getDataPermWarehouse(dto).getData();
		//被操作账号已有的仓库权限
		List<Long> exist=new ArrayList<>();
		boolean flag=false;
		if(ObjectUtil.isNotNull(one)){
			if(one.getAllFlag().equals(CommonConstants.ONE)){
				flag=true;
			}else if(StrUtil.isNotBlank(one.getIds())){
				exist=Convert.toList(Long.class, StrUtil.split(one.getIds(),','));
			}
		}
		if(CollectionUtil.isNotEmpty(list)){
			for (WarehouseFeignVO item:list) {
				ScopeDataVO scopeDataVO=new ScopeDataVO();
				scopeDataVO.setId(item.getId()).setCode(item.getWarehouseCode()).setName(item.getWarehouseName());
				if(siteMap.containsKey(item.getSiteId())){
					scopeDataVO.setParentName(siteMap.get(item.getSiteId()));
				}else {
					SysBusinessEntity sysBusinessEntity = businessEntityMapper.selectById(item.getSiteId());
					scopeDataVO.setParentName(sysBusinessEntity.getBusinessName());
					siteMap.put(item.getSiteId(),sysBusinessEntity.getBusinessName());
				}
				if(flag || exist.stream().filter(v ->v.equals(item.getId())).findFirst().isPresent()){
					scopeDataVO.setChecked(Boolean.TRUE);
				}
				scopeList.add(scopeDataVO);
			}
			deatil.setScopeList(scopeList);
		}
		return deatil;
	}

	@Override
	public void getUserDataPermission(DataPermissionVO vo, DataPermissionVO item) {
		if(CollectionUtil.isNotEmpty(item.getWarehouseList())){
			if(vo.getWarehouseAll()){
				return;
			}else if(item.getWarehouseAll()){
				vo.setWarehouseAll(item.getWarehouseAll());
				vo.setWarehouseList(item.getWarehouseList());
			}else {
				if(CollectionUtil.isNotEmpty(vo.getWarehouseList())){
					List<Long> collect = Stream.of(vo.getWarehouseList(),item.getWarehouseList()).flatMap(Collection::stream).distinct().collect(Collectors.toList());
					vo.setWarehouseList(collect);
				}else {
					vo.setWarehouseList(item.getWarehouseList());
				}
			}
		}
	}

	@Override
	public List<Long> getList(Long tenantId) {
		DataPermissionDTO dto=new DataPermissionDTO();
		dto.setAllFlag(Boolean.TRUE);
		dto.setTenantId(tenantId);
		List<WarehouseFeignVO> list = warehouseProvider.getDataPermWarehouse(dto).getData();
		if(CollectionUtil.isNotEmpty(list)){
			List<Long> ids=list.stream().map(WarehouseFeignVO::getId).collect(Collectors.toList());
			return	ids;
		}
		return null;
	}

	@Override
	public void getAllList(UserRoleScopeVO userRoleScopeVO, SysUserRoleScope scope, Long tenantId) {
		if(CommonConstants.ZERO.equals(scope.getAllFlag())&& StrUtil.isBlank(scope.getIds())){
			return;
		}
		DataPermissionDTO dto=new DataPermissionDTO();
		dto.setTenantId(tenantId);
		if(CommonConstants.ONE.equals(scope.getAllFlag())){
			dto.setAllFlag(Boolean.TRUE);
		}else {
			List<Long> allWarehouseIds= Convert.toList(Long.class, StrUtil.split(scope.getIds(),','));
			dto.setAllFlag(Boolean.FALSE);
			dto.setIds(allWarehouseIds);
		}
		List<WarehouseFeignVO> list = warehouseProvider.getDataPermWarehouse(dto).getData();
		List<ScopeDataVO> scopeList =new ArrayList<>();
		if(CollectionUtil.isNotEmpty(list)){
			for (WarehouseFeignVO item:list) {
				ScopeDataVO scopeDataVO=new ScopeDataVO();
				scopeDataVO.setId(item.getId()).setCode(item.getWarehouseCode()).setName(item.getWarehouseName());
				if(siteMap.containsKey(item.getSiteId())){
					scopeDataVO.setParentName(siteMap.get(item.getSiteId()));
				}else {
					SysBusinessEntity sysBusinessEntity = businessEntityMapper.selectById(item.getSiteId());
					scopeDataVO.setParentName(sysBusinessEntity.getBusinessName());
					siteMap.put(item.getSiteId(),sysBusinessEntity.getBusinessName());
				}
				scopeList.add(scopeDataVO);
			}
			userRoleScopeVO.setScopeList(scopeList);
		}
	}

	@Override
	public Boolean checkPermission(SysUserRoleScope one, String name, Long tenantId) {
		return null;
	}
}
